package controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.FindItemsByKeywordsDAO;
import dao.GetItemDAO;
import databean.ItemBean;

public class FindItemsByKeywordsAction extends Action{

	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) {

		
		//call from api.js
		if (request.getParameter("title")!=null) {
			String title=request.getParameter("title");
			
			List<ItemBean> rs = FindItemsByKeywordsDAO.findItemsByKeywords(title);
			
			if (rs==null) {
				request.setAttribute("error", FindItemsByKeywordsDAO.getMessage());
				return "findItemsByKeywords.view";
			} else {
				request.setAttribute("rq", title);
				request.setAttribute("timestamp", FindItemsByKeywordsDAO.getTimestamp());
				request.setAttribute("rs", rs);
			}
			
			return "findItemsByKeywordsResult.view";
		}
		
		
		
		
		String add=request.getParameter("add");
		
		//case the first time come
		if(add==null||add.length()==0){
			return "findItemsByKeywords.view";
		}
		
		//get user input information
		String keyword=request.getParameter("givenkeyword");
		
		//update to ebay
		if(keyword!=null){
			
			List<ItemBean> rs = FindItemsByKeywordsDAO.findItemsByKeywords(keyword);
			
			if (rs==null) {
				request.setAttribute("error", FindItemsByKeywordsDAO.getMessage());
				return "findItemsByKeywords.view";
			} else {
				request.setAttribute("rq", keyword);
				request.setAttribute("timestamp", FindItemsByKeywordsDAO.getTimestamp());
				request.setAttribute("rs", rs);
			}

		}
		return "findItemsByKeywordsResult.view";
		
	}

}
